package com.ltkj.baseplatform.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import com.ltkj.baseplatform.domain.SerGameType;
import com.ltkj.baseplatform.service.ISerGameTypeService;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.utils.poi.ExcelUtil;
import com.ltkj.common.core.web.page.TableDataInfo;

/**
 * 赛事分类Controller
 * 
 * @author ruoyi
 * @date 2023-09-13
 */
@Api(tags = "赛事分类")
@RestController
@RequestMapping("/gametype")
public class SerGameTypeController extends BaseController
{
    @Autowired
    private ISerGameTypeService serGameTypeService;

    /**
     * 查询赛事分类列表
     */
    //@ApiOperation("查询赛事分类列表")
    //@RequiresPermissions("baseplatform:gametype:list")
    //@GetMapping("/list")
    /*public TableDataInfo list(SerGameType serGameType)
    {
        startPage();
        List<SerGameType> list = serGameTypeService.selectSerGameTypeList(serGameType);
        return getDataTable(list);
    }*/

    /**
     * 查询赛事分类列表
     */
    @ApiOperation("查询赛事分类列表")
    @RequiresPermissions("baseplatform:gametype:list")
    @GetMapping("/list")
    public AjaxResult list(SerGameType serGameType)
    {
        List<SerGameType> list = serGameTypeService.selectSerGameTypeList(serGameType);
        return AjaxResult.success(list);
    }

    /**
     * 导出赛事分类列表
     */
    /*@ApiOperation("导出赛事分类列表")
    @RequiresPermissions("baseplatform:gametype:export")
    @Log(title = "赛事分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SerGameType serGameType)
    {
        List<SerGameType> list = serGameTypeService.selectSerGameTypeList(serGameType);
        ExcelUtil<SerGameType> util = new ExcelUtil<SerGameType>(SerGameType.class);
        util.exportExcel(response, list, "赛事分类数据");
    }*/

    /**
     * 获取赛事分类详细信息
     */
    @ApiOperation("获取赛事分类详细信息")
    @RequiresPermissions("baseplatform:gametype:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(serGameTypeService.selectSerGameTypeById(id));
    }

    /**
     * 新增赛事分类
     */
    @ApiOperation("新增赛事分类")
    @RequiresPermissions("baseplatform:gametype:add")
    @Log(title = "赛事分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SerGameType serGameType)
    {
        return toAjax(serGameTypeService.insertSerGameType(serGameType));
    }

    /**
     * 修改赛事分类
     */
    @ApiOperation("修改赛事分类")
    @RequiresPermissions("baseplatform:gametype:edit")
    @Log(title = "赛事分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SerGameType serGameType)
    {
        return toAjax(serGameTypeService.updateSerGameType(serGameType));
    }

    /**
     * 删除赛事分类
     */
    @ApiOperation("删除赛事分类")
    @RequiresPermissions("baseplatform:gametype:remove")
    @Log(title = "赛事分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        String hasChildStr = "";
        String existDataStr = "";
        for (Long id : ids) {
            if (serGameTypeService.hasChildByTypeId(id)) {
                hasChildStr = hasChildStr + id + ",";
            }
            if (serGameTypeService.checkTypeExistData(id)) {
                existDataStr = existDataStr + id + ",";
            }
        }
        if(StringUtils.hasText(hasChildStr)){
            return warn(hasChildStr + "存在下级节点,不允许删除");
        }
        if(StringUtils.hasText(existDataStr)){
            return warn(existDataStr + "分类下存在赛事,不允许删除");
        }

        return toAjax(serGameTypeService.deleteSerGameTypeByIds(ids));
    }
}
